package org.abm.jwebsite.sitemap;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.abm.jwebsite.utilities.Utilities;

/**
 *
 * @author Saptarshi Debnath (saptarshi.devnath@gmail.com)
 */
public class Navigation {

    private Map<String, String> seo;
    private Map<String, String> navigation;
    private Map<String, String> navigationData;
    private Map<String, String> data;
    private List<Navigation> childNavigation;
    private Navigation parent;

    public Navigation(Map<String, String> seo, Map<String, String> navigation, Map<String, String> navigationData,
            Map<String, String> data, List<Navigation> childNavigation) {
        this.seo = Collections.unmodifiableMap(seo);
        this.navigation = Collections.unmodifiableMap(navigation);
        this.navigationData = Collections.unmodifiableMap(navigationData);
        this.data = Collections.unmodifiableMap(data);
        this.childNavigation = Collections.unmodifiableList(childNavigation);
        this.parent = null;
    }

    public Navigation getParent() {
        return parent;
    }

    public void setParent(Navigation parent) {
        if (this.parent == null) {
            this.parent = parent;
        }
    }

    public Map<String, String> getNavigationData() {
        return navigationData;
    }

    public String getNavigationData(String key) {
        return navigationData.get(key);
    }

    public String getSeo(String key) {
        return this.seo.get(key);
    }

    public Map<String, String> getSeo() {
        return this.seo;
    }

    public String getNavigation(String key) {
        return this.navigation.get(key);
    }

    public Map<String, String> getNavigation() {
        return Collections.unmodifiableMap(this.navigation);
    }

    public String getData(String key) {
        return this.data.get(key);
    }

    public Map<String, String> getData() {
        return this.data;
    }

    public Iterator<Navigation> getChildNavigation() {
        return this.childNavigation.iterator();
    }

    @Override
    public String toString() {
        return this.getContentAsString("");
    }

    private String getContentAsString(String indent) {
        StringBuilder buffer = new StringBuilder();
        buffer.append("-------------------").append(Utilities.INSTANCE.getLineSeperator());
        buffer.append(getValueOfMap(this.seo, indent));
        buffer.append(getValueOfMap(this.navigation, indent));
        buffer.append(getValueOfMap(this.navigationData, indent));
        buffer.append(getValueOfMap(this.data, indent));
        Iterator<Navigation> iterator = this.childNavigation.iterator();
        while (iterator.hasNext()) {
            buffer.append(iterator.next().getContentAsString(indent + "     "));
        }
        return buffer.toString();
    }

    private StringBuilder getValueOfMap(Map<String, String> map, String indent) {
        StringBuilder buffer = new StringBuilder();
        Iterator<String> iterator = map.keySet().iterator();
        String tempKey = null;
        while (iterator.hasNext()) {
            tempKey = iterator.next();
            buffer.append(indent).append(tempKey).append(" : ").append(map.get(tempKey)).append(
                    Utilities.INSTANCE.getLineSeperator());
        }
        return buffer;
    }
}
